﻿
GridClass = function () {
    var self = this;
    function AutoPageSize() {
        alert(window.screen.height);
    }

    self.searchFieldTriggerClick = function (field, trigger, index) {
        var me = field,
            store = me.up("grid").getStore(),
            value = me.getValue();

        if (index == 0) {
            me.setValue('');
            store.clearFilter();
            trigger.hide();
        }
        else {
            if (value.length > 0) {
                if (field.data) {
                    var aoData = {};
                    aoData[field.data.field] = this.value;

                    store.proxy.extraParams = aoData;
                    store.reload();
                }

                me.getTrigger(0).show();
            }
        }
    };
    var onComboBoxSelect = function (combo) {
        var store = combo.up("gridpanel").getStore();

        store.pageSize = parseInt(combo.getValue(), 10);
        store.reload();
    };


    // 模块 Grid 点击
    self.OnModuleGridItemClick = function (view, record, item, index, e, options) {
        App.Main.G.CurrentDataRecordId = record.Id;
    }
    // 模块 Grid 双击
    self.OnModuleGridItemDblClick = function (view, record, item, index, e) {
        var grid = view.up("gridpanel");
        while (grid.ownerCt.getXType() == "grid") {
            grid = grid.ownerCt;
        }

        var toolbar = grid.down("toolbar");

        if (toolbar.getXType() == "toolbar") {

        }

        var bs = toolbar.items;
        for (var i = 0; i < bs.getCount() ; i++) {
            var b = bs.getAt(i);
            if (b.getText() === '浏览') {
                b.handler(b);
                return;
            }
        }
    }
    // 在新窗体中打开模块项
    self.OpenModuleItemWindow = function (button, action, params) {
        var grid = button.up("grid") ? button.up("grid") : button.up("treepanel");

        var selected = (grid) ? grid.getSelectionModel().getSelection() : null;

        if (!params.ShowOnly && (selected) && selected.length === 0) {
            Tip("操作提示", "没有选中任何记录！");
            return;
        }
        var dataId = (selected && selected.length > 0) ? getPkValue(selected[0].getData()) : "";

        Ext.net.directRequest({
            cleanRequest: true,
            url: action + "/" + dataId,
            extraParams: params,
            method: "GET",
            control: this
        });
    }

    // 模块操作
    self.OnModuleOperation = function (button, action, params) {
        if (params == null) params = {};
        var grid = button.up("grid") ? button.up("grid") : button.up("treepanel");
        // 处理等无窗体删除
        if (params.IsAjax) {
            var selected = grid.getSelectionModel().getSelection();
            if (!(selected)) return false;
            if (selected.length === 0) {
                Tip("操作提示", "没有选中任何记录！");
                return false;
            }

            Ext.MessageBox.confirm(button.getText() + '确认', '请确实是要' + button.getText() + '这些记录?', function (btn) {
                if (btn === 'yes') {

                    params.Ids = generaterParamFromSelected(selected);

                    Ext.Ajax.request({
                        url: action,
                        params: params,
                        method: "POST",
                        success: function (response) {

                            var result = Ext.JSON.decode(response.responseText);

                            if (result.Success) {
                                if (result.Message.length > 0)
                                    Tip('提示', result.Message);
                                grid.store.load();
                            } else {
                                Tip('操作失败', result.Errors);
                            }

                        }
                    });
                }
            });
            return true;
        } else {
            self.OpenModuleItemWindow(button, action, params);
            return true;
        }
    }

    // 提示信息
    function Tip(t, c) {
        if (this.window.parent) {
            this.window.parent.Ext.example.msg(t, c);
        } else {
            Ext.example.msg(t, c);
        }
    }

    // 用选择的id构建Post参数对象
    function generaterParamFromSelected(selected) {

        var ids = new Array();

        for (var i = 0; i < selected.length; i++) {
            ids.push(getPkValue(selected[i].getData()));
        }

        return ids;
    }
    function getPkValue(record) {
        if (typeof (record) == "undefined") {
            return null;
        }
        if (record.Id) {
            return record.Id;
        }
        else if (record.ID) {
            return record.ID;
        }
        else {
            return record.id;
        }
    }
    //获取树的勾选值
    self.GetTreeValues = function (obj) {
        var ids = [];
        var tree;
        if (obj.getXType() === "button") {
            tree = obj.up("treepanel");
        } else {
            tree = obj;
        }
        tree.getChecked().forEach(function (item) {
            ids.push(item.raw.id);
        });
        return ids;
    }

    self.dropItem = function (node, data, overModel, dropPosition, options) {
        var ids = [];
        var parentId;
        if (dropPosition === "append") {
            parentId = overModel.raw.id;
            overModel.childNodes.forEach(function (item) {
                ids.push(item.raw.id);
            });
            ids.push(data.records[0].raw.id);
        } else {
            //虚拟根节点没有这个
            if (typeof (overModel.parentNode.raw) != "undefined") {
                parentId = overModel.parentNode.raw.id;
            } else {
                parentId = "";
            }
            overModel.parentNode.childNodes.forEach(function (item) {
                ids.push(item.raw.id);
            });
        }

        var url = data.view.ownerCt.raMoveUrl;

        $.ajax({
            type: "POST",
            //contentType: "application/json, text/json",
            url: url,
            data: {
                'parentId': parentId,
                'ids': ids
            },
            success: function (result) {
                Tip('操作成功', result);
            }
        });
    };
    self.beforedropItem = function (node, data, overModel, dropPosition, dropFunction) {

    };
}
